package com.hngy.xpq.android_practice;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.hngy.xpq.android_practice.bean.User;
import com.hngy.xpq.android_practice.database.SQLiteUser;
import com.hngy.xpq.android_practice.http.TcpClient;
import com.hngy.xpq.android_practice.user_main.UserTime;
import com.hngy.xpq.android_practice.utils.UserOrder;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class OrderUserRegist extends AppCompatActivity {

    private EditText name,key1,key2,phone,add;
    private TextView born;
    private Button zc,time;
    private ImageButton previous;
    SharedPreferences user,Main;
    SQLiteUser sqLiteUser;

    private void RegistControl() {
        name = findViewById(R.id.zc_xpq_name);
        key1 = findViewById(R.id.text_xpq_key);
        key2 = findViewById(R.id.text_xpq_key2);
        phone = findViewById(R.id.text_xpq_phone);
        born = findViewById(R.id.text_xpq_born);
        add = findViewById(R.id.text_xpq_add);
        zc = findViewById(R.id.button_zc);
        time = findViewById(R.id.button_time);
        previous = findViewById(R.id.previous);

        user = super.getSharedPreferences("user",MODE_PRIVATE);
        Main = super.getSharedPreferences("main",MODE_PRIVATE);
        sqLiteUser = new SQLiteUser(this);
    }

    private void RegistShared() {
        name.setText(user.getString("name",""));
        key1.setText(user.getString("key1",""));
        key2.setText(user.getString("key2",""));
        phone.setText(user.getString("phone",""));
    }

    public void IndexToast(String text){
        Toast.makeText(OrderUserRegist.this,text,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_zc_regist);
        RegistControl();
        RegistShared();
        Intent intent = getIntent();
        String index_time = intent.getStringExtra("time");
        born.setText(index_time);
        zc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                User u = new User();
                if (TextUtils.isEmpty(name.getText())){
                    IndexToast("用户名不能为空!!");
                } else if (TextUtils.isEmpty(key1.getText())){
                    IndexToast("密码不能为空!!");
                } else if (TextUtils.isEmpty(key2.getText())){
                    IndexToast("确认密码不能为空!!");
                } else if (!key1.getText().toString().equals(key2.getText().toString())){
                    IndexToast("两次密码不一致!!");
                } else if (TextUtils.isEmpty(phone.getText())){
                    IndexToast("联系方式不能为空!!");
                } else if (TextUtils.isEmpty(born.getText())){
                    IndexToast("出生年月不能为空!!");
                } else if (TextUtils.isEmpty(add.getText())){
                    IndexToast("地址不能为空!!");
                } else {
                    //本地数据库保存信息,节约流量
                    u.setName(name.getText().toString());
                    u.setKey(key1.getText().toString());
                    u.setPhone(phone.getText().toString());
                    u.setBorn(born.getText().toString());

                    String index = name.getText().toString();
                    //用户端发送注册信息给服务器
                    String information = "ZC001//" + name.getText().toString().trim() + "//" +
                            key1.getText().toString().trim() + "//男//" + born.getText().toString().trim() + "//" +
                            phone.getText().toString().trim() + "//" + add.getText().toString().trim();
                    //处理结果
                    String a = new TcpClient().Transits(information);
                    IndexToast(a);
                    if (!TextUtils.isEmpty(a) && a.equals("注册成功") && sqLiteUser.addData(u)) {
                        //新账号的本地信息缓存，节约空间
                        SharedPreferences.Editor editor1 = Main.edit();
                        editor1.putString("name",index);
                        editor1.putString("key" , null);
                        editor1.putString("check","0");
                        editor1.commit();
                        Intent intent = new Intent(OrderUserRegist.this,OrderActivity.class);
                        startActivity(intent);
                        //清空注册信息的缓存
                        SharedPreferences.Editor editor = user.edit();
                        editor.putString("name","");
                        editor.putString("key1","");
                        editor.putString("key2","");
                        editor.putString("phone","");
                        editor.commit();
                        finish();
                    }
                }
            }
        });
        time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = user.edit();
                editor.putString("name",name.getText().toString());
                editor.putString("key1",key1.getText().toString());
                editor.putString("key2",key2.getText().toString());
                editor.putString("phone",phone.getText().toString());
                editor.commit();
                Intent intent = new Intent(OrderUserRegist.this, UserTime.class);
                startActivity(intent);
            }
        });
        previous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {    //清空文件缓存
                SharedPreferences.Editor editor = user.edit();
                editor.putString("name","");
                editor.putString("key1","");
                editor.putString("key2","");
                editor.putString("phone","");
                editor.commit();
                finish();
            }
        });
    }
}